import Vue from 'vue'
import Router from 'vue-router'
import Login from '../views/login.vue'
import Home from '../views/home.vue'
import Forget from '../views/forget.vue'
import Register from '../views/register.vue'

import User from '../views/user/user.vue'
import Admin from '../views/admin/admin.vue'
import Shop from '../views/shop/shop.vue'
import Posters from '../views/shop/posters.vue'
import Chart from '../views/chart/chart.vue'
import Dish from '../views/dish/dish.vue'
import Order from '../views/order/order.vue'
import Comment from '../views/order/comment.vue'
import Classify from '../views/dish/classify.vue'

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';


Vue.use(Router)
Vue.use(ElementUI);
const routes = []

const router = new Router({
    routes: [{
            path: '/',
            redirect: '/login'
        },
        {
            path: '/login',
            component: Login
        }, {
            path: '/home',
            component: Home,
            redirect: '/user',
            children: [{
                    path: '/user',
                    component: User
                },
                {
                    path: '/admin',
                    component: Admin
                },
                {
                    path: '/shop',
                    component: Shop
                },
                {
                    path: '/posters',
                    component: Posters
                },
                {
                    path: '/dish',
                    component: Dish
                },
                {
                    path: '/classify',
                    component: Classify
                },
                {
                    path: '/chart',
                    component: Chart
                },
                {
                    path: '/order',
                    component: Order
                },
                {
                    path: '/comment',
                    component: Comment
                },

            ]
        }, {
            path: '/forget',
            component: Forget
        }, {
            path: '/register',
            component: Register
        }
    ]
})

//路由导航守卫
router.beforeEach((to, from, next) => {
    if (to.path == '/login' || to.path == '/forget' || to.path == '/register') return next()
    const token = window.sessionStorage.getItem("token")
    if (!token) {
        ElementUI.Message({
            message: '请先进行登录',
            type: 'error'
        });
        return next("/login")
    }
    next()

})




export default router